#ifndef TRACKER_H
#define TRACKER_H

//#include "dpvs.h"
#include <eq/eq.h>
#include <string>
#include "body.h"

namespace eqPc
{	


	class FlyStick;
	class Body;
	class Head;

    class Tracker
    {
    public:
        Tracker();
		~Tracker()
		{
			if(_running){
				delete dtrack;			
				delete head;
				delete flyStick;
				dtrack=0;head=0;flyStick=0;
			}
		}

        bool init( const std::string& port );

		void setWorldMatrix(const eq::Matrix4f& matrix)
			{ _worldTriggerMatrix = matrix; }

        void setWorldToEmitter( const eq::Matrix4f& matrix )
            { _worldToEmitter = matrix; }

        void setSensorToObject( const eq::Matrix4f& matrix )
            { _sensorToObject = matrix; }

        bool isRunning() const 
			{ return _running; }

        bool update();

		eq::Matrix4f& getTransformationMatrix(BodyType type);
		const eq::Matrix4f& getDeltaTransformation(BodyType type, bool relativeToWorld);
		
		ButtonState getButtonState(Button button);


    private:
        bool _update(); //update without state checking
        bool _read( unsigned char* buffer, const size_t size,
                    const unsigned long int timeout );
		void _errorToConsole();
		void _outputToConsole(DTrack_Body_Type_d &body);
		void _updateHead(DTrack_Body_Type_d &body);
		void _updateFlyStick(DTrack_FlyStick_Type_d &flystick);
		

        bool  _running;

		DTrackSDK* dtrack;

		Head* head;
		FlyStick* flyStick;

		eq::Matrix4f _worldTriggerMatrix;

        /** world to emitter transformation */
        eq::Matrix4f _worldToEmitter;
        /** sensor to object transformation */
        eq::Matrix4f _sensorToObject;
    };
}

#endif // FOB_TRACKER_H
